* build_mw_measures_gap.do	JEP 20/02/20
* This file builds on prelim_est*.do, builds a program to generate the minimum wage measures.

* 1. Choose a sample and a measure
* 2. Calculate minimum wage measures
* 3. Create treatment variables and trends
* 4. Run regressions

*************************** Change log ********************************** 

/*
	2/20/20	This version adds indexes V1 and V2, for the fraction affected.
	
*/
						
****** 2. Calculate minimum wage measures

cap program drop mw_measures_gap
program mw_measures_gap
	syntax varname, stub(string) [time(string)]
	
	if "`time'"=="" loc time tq(1998q4)
	
	* 2.1 Minimum wage bite
	* Bite has to be percentage of people earning between the old real minimum wage and the new real minimum wage each period.
	* Do I use the per city real minimum wage or the countrywide one?
	* Use the per-city, to be net of differential inflation

	* Bite in t: % of people earning above minw in t and below minw in t+1
	* If the real minimum wage falls, then the bite is the percentage people above minw t+1 and below minw in t
	* But then the correlation between the bite and avg minimum wages would be negative over that period (larger bites, larger avg falls)
	* Take bite only for the 1998 end of year increase. This would be equivalent to the 0/1 classification at that point if I were running a standard diff in diff

	* Create several "bite" measures

	* Affected variables 
		
	gen af_bite_`stub'=0
	gen af_bite_e_`stub'=0
	gen af_bite_fb_`stub'=0
	gen af_gap_`stub' = 0
	gen af_sqgap_`stub' = 0
	gen af_gapb_`stub' = 0
	gen af_sqgapb_`stub' = 0
	
	* 1. Just bite
	* To rescale for interpretation, bites are multiplied by 100, so af is 100 or 0. This way I don't need to rescale the bites afterwards.
	* If real mw is increasing
	replace af_bite_`stub'=100 if salario_mensual_real>=minw_mensual_real & salario_mensual_real<=minw_mensual_real_f1 & minw_mensual_real<=minw_mensual_real_f1
	* If real mw is decreasing
	replace af_bite_`stub'=100 if af_bite_`stub'==0 & salario_mensual_real<=minw_mensual_real & salario_mensual_real>=minw_mensual_real_f1 & minw_mensual_real>=minw_mensual_real_f1
	* 2. Extended bite (incidencia modal)
	replace af_bite_e_`stub'=100 if salario_mensual_real>=0.9*minw_mensual_real & salario_mensual_real<=1.1*minw_mensual_real_f1 & minw_mensual_real<=minw_mensual_real_f1
	replace af_bite_e_`stub'=100 if af_bite_e_`stub'==0 & salario_mensual_real<=0.9*minw_mensual_real & salario_mensual_real<=1.1*minw_mensual_real_f1 & minw_mensual_real>=minw_mensual_real_f1
	* 3. Fraction below minimum
	replace af_bite_fb_`stub'=100 if salario_mensual_real<=minw_mensual_real_f1 & minw_mensual_real<=minw_mensual_real_f1
	replace af_bite_fb_`stub'=100 if af_bite_fb_`stub'==0 & salario_mensual_real>=minw_mensual_real_f1 & minw_mensual_real>=minw_mensual_real_f1
	* 4. V1, gap to mw for those affected
	replace af_gap_`stub' = (minw_mensual_real_f1 - salario_mensual_real)/minw_mensual_real_f1 if af_bite_`stub' == 100
	* 5. V2, squared gap to mw for those affected
	replace af_sqgap_`stub' = ((minw_mensual_real_f1 - salario_mensual_real)/minw_mensual_real_f1)^2 if af_bite_`stub' == 100
	* 6. V1, gap to mw for all below
	replace af_gapb_`stub' = (minw_mensual_real_f1 - salario_mensual_real)/minw_mensual_real_f1 if af_bite_fb_`stub' == 100
	* 7. V2, squared gap for all below
	replace af_sqgapb_`stub' = ((minw_mensual_real_f1 - salario_mensual_real)/minw_mensual_real_f1)^2 if af_bite_fb_`stub' == 100
	
	
	* Nominal bites
	
	gen af_nom_bite_`stub'=0
	gen af_nom_bite_e_`stub'=0
	gen af_nom_bite_fb_`stub'=0
	
	* 1. Just bite
	* To rescale for interpretation, bites are multiplied by 100, so af is 100 or 0. This way I don't need to rescale the bites afterwards.
	replace af_nom_bite_`stub'=100 if salario_mensual>=minw & salario_mensual<=minw_f1 & minw<=minw_f1
	* 2. Extended bite (incidencia modal)
	replace af_nom_bite_e_`stub'=100 if salario_mensual>=0.9*minw & salario_mensual<=1.1*minw_f1 & minw<=minw_f1
	* 3. Fraction below minimum
	replace af_nom_bite_fb_`stub'=100 if salario_mensual<=minw_f1 & minw<=minw_f1
	
	
	* Nominal bites, accounting for rounding	
	
	gen af_nom_round_bite_`stub'=0
	gen af_nom_round_bite_e_`stub'=0
	gen af_nom_round_bite_fb_`stub'=0
	
	replace af_nom_round_bite_`stub'=100 if salario_mensual_round>=minw_round & salario_mensual_round<=minw_round_f1 & minw_round<=minw_round_f1
	replace af_nom_round_bite_`stub'=100 if af_nom_round_bite_`stub'==0 & salario_mensual_round<=minw_round & salario_mensual_round>=minw_round_f1 & minw_round>=minw_round_f1
	* 2. Extended bite (incidencia modal)
	replace af_nom_round_bite_e_`stub'=100 if salario_mensual_round>=0.9*minw_round & salario_mensual_round<=1.1*minw_round_f1 & minw_round<=minw_round_f1
	replace af_nom_round_bite_e_`stub'=100 if af_nom_round_bite_e_`stub'==0 & salario_mensual_round<=0.9*minw_round & salario_mensual_round>=1.1*minw_round_f1 & minw_round>=minw_round_f1
	* 3. Fraction below minimum
	replace af_nom_round_bite_fb_`stub'=100 if salario_mensual_round<=minw_round_f1 & minw_round<=minw_round_f1
	replace af_nom_round_bite_fb_`stub'=100 if af_nom_round_bite_fb_`stub'==0 & salario_mensual_round>=minw_round_f1 & minw_round>=minw_round_f1
	
	
	
	/* tabulate ciudad time, summ(af) means */
	
	* Make list of variables to collapse
	glo to_coll ""
	glo to_collw ""
	foreach pre in _ _nom_ _nom_round_ {
		foreach post in _ _e_ _fb_	{
			glo to_coll "$to_coll `pre'bite`post'`stub'=af`pre'bite`post'`stub'"
			glo to_collw "$to_collw `pre'bite`post'`stub'=af`pre'bite`post'`stub'"
		}
	}
	glo to_coll "$to_coll gap_`stub' = af_gap_`stub' sqgap_`stub' = af_sqgap_`stub'"
	glo to_collw "$to_collw gap_`stub' = af_gap_`stub' sqgap_`stub' = af_sqgap_`stub'"
	glo to_coll "$to_coll gapb_`stub' = af_gapb_`stub' sqgapb_`stub' = af_sqgapb_`stub'"
	glo to_collw "$to_collw gapb_`stub' = af_gapb_`stub' sqgapb_`stub' = af_sqgapb_`stub'"
	
	
	* Collapse by mean gives the percentage affected
	* By city
	preserve
	collapse (mean) ${to_coll} if `1', by(ccode time)
	rename _* *
	* Keep the bites right before the MW jump
	xtset ccode time
	keep if time==`time'
	tempfile bit
	save `bit'
	restore
	
	
	* By city industry
	preserve
	collapse (mean) ${to_coll} if `1', by(ccode cactividad_empresa time)
	rename _* *
	* Keep the bites right before the MW jump
	keep if time==`time'
	rename *bite* *bite*_ind
	rename *gap* *gap*_ind
	tempfile bitind
	save `bitind'
	restore
	
	
	
	
	* By industry only
	preserve
	collapse (mean) ${to_coll} if `1', by(cactividad_empresa time)
	rename _* *
	* Keep the bites right before the MW jump
	keep if time==`time'
	tempfile bitindo
	rename *bite* *bite*_indo
	rename *gap* *gap*_indo
	save `bitindo'
	restore
	
	merge n:1 ccode using `bit'
	drop _merge
	
	
	merge n:1 ccode cactividad_empresa using `bitind'
	drop _merge
	
	
	
	merge n:1 cactividad_empresa using `bitindo'
	drop _merge
	
	
	
	

	* Generate weighted bite measures (not used)
	
	* By city
	preserve
	collapse (mean)${to_collw} if `1' [iw=fex], by(ccode time)
	rename _* *
	xtset ccode time
	keep if time==`time'
	tempfile bit
	save `bit'
	restore
	
	* By city industry
	preserve
	collapse (mean)${to_collw} if `1' [iw=fex], by(ccode cactividad_empresa time)
	rename _* *
	keep if time==`time'
	rename *bite* *bite*_ind
	tempfile bitind
	save `bitind'
	restore
	
	* By industry only
	preserve
	collapse (mean)${to_collw} if `1' [iw=fex], by(cactividad_empresa time)
	rename _* *
	keep if time==`time'
	rename *bite* *bite*_indo
	tempfile bitindo
	save `bitindo'
	restore
	
	merge n:1 ccode using `bit'
	drop _merge

	merge n:1 ccode cactividad_empresa using `bitind'
	drop _merge
	merge n:1 cactividad_empresa using `bitindo'
	drop _merge
	
	
	
	* 2.2 Minimum to median
	* To make it forward looking as the bite, it has to be the next mw relative to the median
	egen salario_real_mensual_med_`stub' = median(salario_mensual_real) if `1', by(ccode cactividad_empresa time)
	gen mw_to_medw_`stub' = minw_mensual_real_f1/salario_real_mensual_med if `1'
	levelsof ccode, local(c)
	levelsof cactividad_empresa, local(a)
	foreach city in `c' {
		foreach aa in `a' {
			sum mw_to_medw_`stub' if time==`time' & ccode==`city' & cactividad==`aa' & `1', meanonly
			replace mw_to_medw_`stub'=r(mean) if ccode==`city' & cactividad==`aa' & `1'
		}
	}

	/* tabulate ciudad time, summ(mw_to_medw) means */

	/* * How correlated are the measures?
	preserve
	collapse (firstnm) bite mw_to_medw, by(ciudad time)
	bys ciudad: corr bite mw_to_medw
	line bite mw_to_medw time if ciudad=="BOGOTÁ"
	restore */

	
	
	
end

* save "`master'/Data/Source/ocupados_mw_bites.dta", replace

* Project output
*project, creates("`master'/Data/Source/ocupados_mw_bites.dta")


